package com.remotecontrol.invokers;

import com.remotecontrol.commands.Command;
import com.remotecontrol.commands.NoCommand;

public class RemoteControl {
	private Command[] onCommands;
	private Command[] offCommands;
	private Command undoCommand;

	public RemoteControl() {
		onCommands = new Command[7];
		offCommands = new Command[7];

		Command noCommand = new NoCommand();
		for (int i = 0; i < 7; i++) {
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
		undoCommand = noCommand;
	}

	public void setCommand(int slot, Command onCommand, Command offCommand) {
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}

	public void onButtonWasPushed(int slot) {
		onCommands[slot].execute();
		undoCommand = onCommands[slot];
	}

	public void offButtonWasPushed(int slot) {
		offCommands[slot].execute();
		undoCommand = offCommands[slot];
	}
	
	public void undoButtonsWasPushed() {
		undoCommand.undo();
	}

	public String toString() {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < onCommands.length; i++) {
			stringBuilder.append("[slot ").append(i).append("] ")
					.append(onCommands[i].getClass().getName()).append("     ")
					.append(offCommands[i].getClass().getName()).append("\n");
		}
		
		return stringBuilder.toString();
	}
}
